package com.example.c201901090104;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class RebangFragment extends Fragment {
    RecyclerView recyclerView;
    List<Rebang.ListDTO> rebanglist = new ArrayList();
    MyAdapter1 myAdapter;
    SmartRefreshLayout smartRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_rebang, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageView imageView=getActivity().findViewById(R.id.imageView7);
        Glide.with(getActivity())
                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp1.itc.cn%2Fq_70%2Fimages03%2F20200611%2F32f2c14003f34a2eba5f6f875afc4341.png&refer=http%3A%2F%2Fp1.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1626461711&t=e056917fde0696a94e4912d37180ecf1")
                .into(imageView);
        recyclerView = getActivity().findViewById(R.id.recyclerView1);
        smartRefreshLayout = getActivity().findViewById(R.id.smartRefreshLayout1);
        rebanglist.clear();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://tenapi.cn/resou/");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                Rebang rebang = gson.fromJson(result, Rebang.class);
                                Log.i("Main", result);
                                for (int i = 0; i < rebang.list.size(); i++) {
                                    rebanglist.add(rebang.list.get(i));
                                }

                                myAdapter = new MyAdapter1();
                                recyclerView.setAdapter(myAdapter);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("Main", "error");
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
        thread.start();/*
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()));
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                rebanglist.clear();
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://tenapi.cn/resou/");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        Rebang rebang = gson.fromJson(result, Rebang.class);
                        for (int i = 0; i < rebang.list1.size(); i++) {
                            rebanglist.add(rebang.list1.get(i));
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
                refreshLayout.finishRefresh(1000);
            }
        });*/

    }

    public class MyAdapter1 extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.list2_item, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            int num=position+1;
            holder.rank.setText(""+num);
            holder.title.setText(rebanglist.get(position).name);
            holder.hot.setText(""+rebanglist.get(position).hot);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), Click_news.class);
                    intent.putExtra("url", rebanglist.get(position).url);
                    startActivity(intent);
                    //Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return rebanglist.size();
        }


    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView rank;
        TextView title;
        TextView hot;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            rank = itemView.findViewById(R.id.rankView1);
            title = itemView.findViewById(R.id.titleView1);
            hot=itemView.findViewById(R.id.reduView);
        }
    }

}